﻿@model BillBox.Models.CollectionsReportModel
@{
    ViewBag.Title = "Daily Collections Report";
    Layout = "~/Views/Shared/_Layout.cshtml";

    var collections = ViewBag.Collections as IEnumerable<BillBox.Models.CollectionsReportModel>;

    var subscribers = ViewBag.Subscribers as IEnumerable<BillBox.Models.Subscriber>;
    var agents = ViewBag.Agents as IEnumerable<BillBox.Models.Agent>;
    var branches = ViewBag.Branches as IEnumerable<BillBox.Models.AgentBranch>;
}

<div class="row main">
    <div class="col-lg-12">
        <ol class="breadcrumb">
            <li>@Html.ActionLink("Home", "Index", "Default")</li>
            <li class="active">Reports</li>
        </ol>
        <div class="panel panel-default">
            <div class="panel-heading">
                Collections Report
            </div>
            <div class="panel-body">
                @using (Html.BeginForm("Collections", "Report", FormMethod.Get, new { role = "form", @class = "form-horizontal" }))
                {
                    @Html.ValidationSummary(true)

                    @Html.Hidden("generate", true)

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Parameters
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-6">
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.DateRangeFrom, new { @class = "col-sm-5 control-label" })
                                        <div class="col-sm-7">
                                            @Html.TextBoxFor(model => model.DateRangeFrom, new { @class = "form-control" })
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.DateRangeTo, new { @class = "col-sm-5 control-label" })
                                        <div class="col-sm-7">
                                            @Html.TextBoxFor(model => model.DateRangeTo, new { @class = "form-control" })
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.Subscriber, new { @class = "col-sm-5 control-label" })
                                        <div class="col-sm-7">
                                            @Html.DropDownListFor(model => model.Subscriber, new SelectList(subscribers, "Name", "Name"), "All", new { @class = "form-control" })
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.Agent, new { @class = "col-sm-5 control-label" })
                                        <div class="col-sm-7">
                                            @Html.DropDownListFor(model => model.Agent, new SelectList(agents, "Name", "Name"), "All", new { @class = "form-control" })
                                        </div>
                                    </div>

                                    <div class="form-group" id="agent-branches-area">
                                        @if (branches != null)
                                        {
                                            @Html.LabelFor(model => model.Branch, new { @class = "col-sm-5 control-label" })
                                            <div class="col-sm-7">
                                                @Html.DropDownListFor(model => model.Branch, new SelectList(branches, "Name", "Name"), "All", new { @class = "form-control" })
                                            </div>
                                        }
                                    </div>

                                    <div class="form-group">
                                        <div class="col-sm-offset-5 col-sm-7">
                                            <button type="submit" class="btn btn-default">Generate</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        @*<div class="panel-footer">
                                <div class="buttons bottom">
                                    <input type="submit" class="btn btn-default" value="Generate" />
                                </div>
                            </div>*@
                    </div>
                }
                @if (collections != null)
                {
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Results
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive"></div>
                            <table class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>@Html.LabelFor(model => model.BillId)</th>
                                        <th>@Html.LabelFor(model => model.Date)</th>
                                        <th>@Html.LabelFor(model => model.Subscriber)</th>
                                        <th>@Html.LabelFor(model => model.Agent)</th>
                                        <th>@Html.LabelFor(model => model.Branch)</th>
                                        <th style="text-align: right">@Html.LabelFor(model => model.Amount)</th>
                                        <th>@Html.LabelFor(model => model.ProcessingFee)</th>
                                        <th>@Html.LabelFor(model => model.ProcessingFeeGCT)</th>
                                        <th>@Html.LabelFor(model => model.Commission)</th>
                                        <th>@Html.LabelFor(model => model.CommissionGCT)</th>
                                    </tr>
                                </thead>
                                @foreach (var collection in collections)
                                {
                                    <tr>
                                        <td><a href="@Url.Action("ViewReceipt", "Payment", new { billId = collection.BillId })" target="_blank">@collection.BillId.ToString()</a></td>
                                        <td>@collection.Date.ToShortDateString()</td>
                                        <td>@collection.Subscriber</td>
                                        <td>@collection.Agent</td>
                                        <td>@collection.Branch</td>
                                        <td style="text-align: right">@BillBox.Common.Util.DisplayForDollar(collection.Amount)</td>
                                        <td>@BillBox.Common.Util.DisplayForDollar(collection.ProcessingFee)</td>
                                        <td>@BillBox.Common.Util.DisplayForDollar(collection.ProcessingFeeGCT)</td>
                                        <td>@BillBox.Common.Util.DisplayForDollar(collection.Commission)</td>
                                        <td>@BillBox.Common.Util.DisplayForDollar(collection.CommissionGCT)</td>
                                    </tr>
                                }
                            </table>


                            @if (Model.HasPreviousPage)
                            {
                                ViewBag.Previous = Url.Action("Collections", new { generate = true, DateRangeTo = Model.DateRangeTo, DateRangeFrom = Model.DateRangeFrom, Subscriber = Model.Subscriber, Agent = Model.Agent, Branch = Model.Branch, page = Model.PageNumber - 1 });
                            }


                            @if (Model.HasNextPage)
                            {
                                ViewBag.Next = Url.Action("Collections", new { generate = true, DateRangeTo = Model.DateRangeTo, DateRangeFrom = Model.DateRangeFrom, Subscriber = Model.Subscriber, Agent = Model.Agent, Branch = Model.Branch, page = Model.PageNumber + 1 });
                            }

                            @{
                            ViewBag.RecordTotal = Model.Count;
                            ViewBag.RecordBegin = ((Model.PageNumber - 1) * Model.PageSize) + 1;
                            ViewBag.RecordEnd = (Model.PageNumber * Model.PageSize) < Model.Count ? (Model.PageNumber * Model.PageSize) : Model.Count;
                            }
                        </div>
                        <div class="panel-footer">
                            @Html.Partial("_Pager")
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>

</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(document).ready(function () {

            $('#@Html.IdFor(model => model.Agent)').change(function () {
                loadAgentBranches($(this).val());
            });

            var dpProperties = new Object({
                changeMonth: true,
                changeYear: true,
                //dateFormat: "yy-mm-dd",
                defaultDate: +0,
                maxDate: 0
            });

            $("#@Html.IdFor(m => m.DateRangeFrom)").datepicker(dpProperties);
            $("#@Html.IdFor(m => m.DateRangeTo)").datepicker(dpProperties);
        });

        function loadAgentBranches(agent) {
            if (agent) {
                $.ajax({
                    cache: false,
                    type: 'GET',
                    url: '@Url.Action("Branches", "Report")',
                    data: { 'agent': agent },
                    success: function (html) {
                        $("#agent-branches-area").html(html);
                    },
                    error: function () {
                        alert("Error loading agent branches");
                        $("#agent-branches-area").html("");
                    }
                });
            } else {
                $("#agent-branches-area").html("");
            }
        }
    </script>
}



